<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/
	
class Toolbox {
	
	private $db_access;

	public function __construct() {
		$this->db_access = new DbAccessor();
	}

	public function set_db_accessor($accessor) {
		$this->db_access = $accessor;
	}

	public function close() {
		$this->db_access->close();
	}

	//TODO: move this function to MenuMaker or Menu class
	public function util_set_ignored($dir, $menu) {
		$menu[$dir]['ignored'] = true;
		if (is_array($menu[$dir]['children']) and count($menu[$dir]['children'])) {
			foreach($menu[$dir]['children'] as $c) {
				$menu = $this->util_set_ignored($c, $menu);
			}
		}
		return $menu;
	}

	/**
	 * fetch & set moderators for directory
	 * INSECURE but NON-VITAL --> requires MODERATE to be access_action_id #4 and MODERATE to be applied only to groups
	 * TODO: find out what previous comment means.
	 */
	function fetch_moderators($page) {
		$moderators = array();
		
		for ($i = $page->dir_depth - 1; $i >= 0; $i--) {
			$dir_id = $page->directory_tree[$i]['dir_id'];
			$query = $this->db_access->db_query_select(
					array(array(), "MEMBER.nick"), 
					array("(ACCESS_RULE INNER JOIN IN_USERGROUP ON ACCESS_RULE.usergroup_id = IN_USERGROUP.usergroup_id) " .
							"INNER JOIN MEMBER ON IN_USERGROUP.user_id = MEMBER.user_id", "DUMMY"), 
					"action_id = 4 AND dir_id = $dir_id", "nick ASC");
			$sqlresult = $this->db_access->db_do_query($query);
			while ($mod = $this->db_access->db_fetch_array($sqlresult)) {
				if (!in_array($mod['nick'], $moderators)) {
					$moderators[] = $mod['nick'];
				}
			}
		}
		
		$page->moderators = $moderators;
	}

	/**
	 * redirects to the supplied url immediately
	 */
	function redirect($url) {
		session_write_close();
		header("Location: $url");
		exit();
	}


	/**
	 * simple stat function
	 */
	function getmicrotime() { //TODO: correct method name to standard
		list($usec, $sec) = explode(" ", microtime()); 
		return ((float)$usec + (float)$sec); 
	}

	/**
	 * adds an action to the current action array
	 */
	function add_action($page, $id, $type, $action, $javascript = '', $is_major = 0) {
		if (!is_array($page->actions)) {
			$page->actions = array();
		}
		switch($type) {
			case 'url':
				$action = "{$page->dir_path}/$action";
				break;
			case 'url-action':
				$action = "?action=$action";
				break;
		}
		array_unshift($page->actions, array('id' => $id, 'type' => $type, 'action' => $action, 'javascript' => $javascript, 'major' => $is_major));
	}

	function get_keys_and_values($keyfieldname, $valuefieldname, $table, $where = 1, $order = '') {
		$sql = $this->db_access->db_query_select(array(array($keyfieldname, $valuefieldname)), array($table), $where, $order);
		$sqlresult = $this->db_access->db_do_query($sql);
		$keys = array();
		$vals = array();
		while ($r = $this->db_access->db_fetch_array($sqlresult)) {
			array_unshift($keys, $r[$keyfieldname]);
			array_unshift($vals, $r[$valuefieldname]);
		}
		return array('keys' => $keys, 'values' => $vals);
	}

	function get_keyed_values($keyfieldname, $valuefieldname, $table, $where = 1, $order = '') {
		$sql = $this->db_access->db_query_select(array(array($keyfieldname, $valuefieldname)), 
				array($table), $where, $order);
		$sqlresult = $this->db_access->db_do_query($sql);
		$vals = array();
		while ($r = $this->db_access->db_fetch_array($sqlresult)) {
			$vals[$r[$keyfieldname]] = $r[$valuefieldname];
		}
		return $vals;
	}

	function add_log($string) {
		$sql = $this->db_access->db_query_insert(
				array('log_date', 'log', 'ipstring'), 
				array('NOW(14)', 
						$this->db_access->db_sqlize($string), 
						$this->db_access->db_sqlize($_SERVER["REMOTE_ADDR"]." - ".$_SERVER["HTTP_X_FORWARDED_FOR"])), 'LOG');
		$this->db_access->db_do_query($sql);
	}

	function endpage($page, $str, $title="") {
		global $no_menu, $CONFIG;
		include_once("sys/skins/{$CONFIG['default_skin']}/main.inc");
		$page->title = $title ? $title : "Resultaat van uw bewerking";
		$page->content['body'] = $str;
		$page->footer = '';

		print_header($page, $no_menu);
		print_body($page);
		print_footer($page, 0, $no_menu, 1);
		die($page->main);
	}

	function get_last_clicks() {
		$online = $this->db_access->db_do_query($this->db_access->db_query_select(array(array("user_id", "nick")), array("MEMBER"), 
							  "user_id != {$_SESSION['user_id']} AND (DATE_ADD(last_click, INTERVAL 10 MINUTE) > NOW(14))", "nick ASC"));
		$ol = array();
		if ($this->db_access->db_num_rows($online)) {
			while($m = $this->db_access->db_fetch_array($online)) {
				$ol[] = $m;
			}
		}
		return $ol;
	}

	function click() {
		$this->db_access->db_do_query($this->db_access->db_query_update(array('member_since', 'last_visit', 'last_click'), array('member_since', 'last_visit', 'NOW(14)'), "MEMBER", 
						"user_id = {$_SESSION['user_id']}"));
	}
}

?>
